﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Net;
using System.Net.Mail;

namespace XCI.Mail
{
    /// <summary>
    /// 邮件发送组件实现类
    /// </summary>
    public class MailSmtpClient : IMail
    {
        /// <summary>
        /// 邮件发送配置
        /// </summary>
        public static MailSetting Setting
        {
            get { return MailSetting.Default; }
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="recipients">收件人(多个逗号隔开)</param>
        /// <param name="subject">标题</param>
        /// <param name="body">正文</param>
        /// <returns>发送成功返回True</returns>
        public bool Send(string recipients, string subject, string body)
        {
            MailMessage entity = new MailMessage(Setting.From, recipients, subject, body);
            entity.IsBodyHtml = true;
            return Send(entity);
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="entity">邮件实体</param>
        /// <returns>发送成功返回True</returns>
        public bool Send(MailMessage entity)
        {
            var smtpClient = ConfigClient(entity);
            smtpClient.Send(entity);
            return true;
        }

        /// <summary>
        /// 异步发送邮件
        /// </summary>
        /// <param name="recipients">收件人(多个逗号隔开)</param>
        /// <param name="subject">标题</param>
        /// <param name="body">正文</param>
        public void SendAsync(string recipients, string subject, string body)
        {
            MailMessage entity = new MailMessage(Setting.From, recipients, subject, body);
            entity.IsBodyHtml = true;
            SendAsync(entity, null, null);
        }

        /// <summary>
        /// 异步发送邮件
        /// </summary>
        /// <param name="entity">邮件实体</param>
        /// <param name="userToken">一个用户定义对象，此对象将被传递给完成异步操作时所调用的方法</param>
        /// <param name="sendCompletedCallback">在异步电子邮件发送操作完成时回调</param>
        public void SendAsync(MailMessage entity, object userToken, Action<System.ComponentModel.AsyncCompletedEventArgs> sendCompletedCallback)
        {
            var smtpClient = ConfigClient(entity);
            SendCompletedEventHandler completedCallback = (o, e) =>
            {
                sendCompletedCallback?.Invoke(e);
            };
            smtpClient.SendCompleted += completedCallback;
            smtpClient.SendAsync(entity, userToken);
        }

        /// <summary>
        /// 获取协议对象
        /// </summary>
        /// <param name="entity">邮件实体</param>
        protected SmtpClient ConfigClient(MailMessage entity)
        {
            var smtpClient = new SmtpClient();
            smtpClient.Host = Setting.SmtpServer;
            smtpClient.Port = Setting.Port;
            if (Setting.IsAuthenticationRequired)
            {
                smtpClient.UseDefaultCredentials = false;
                string pwd = Setting.AuthenticationPassword;
                smtpClient.Credentials = new NetworkCredential(Setting.AuthenticationUserName, pwd);
            }
            smtpClient.Timeout = Setting.Timeout;
            smtpClient.EnableSsl = Setting.EnableSsl;
            return smtpClient;
        }
    }
}
